﻿/******************************************************************************
 *
 * @copyright:          Copyright 2011 UniSky Ltd.
 * @file:
 * @author:              kenping.liu
 * @creation:
 * @description:
 *
 * @modifications
 * date         user                description
 *
 *
 * @more-details
 *
 *****************************************************************************/
#ifndef _UNISKY_IO_H_
#define _UNISKY_IO_H_

#include "../u_inc.h"
#include "u_util_str.h"

U_NAMESPACE_BEGIN(u)
U_NAMESPACE_BEGIN(io)

typedef struct _st_u_file_time_
{
    DWORD high;
    DWORD low;
}U_FILE_TIME;

typedef struct _st_path_
{
    char dir[MAX_PATH];
    char file[MAX_PATH];
}PATH;

typedef struct _st_file_exe
{
	char file[MAX_PATH];
	char exe[MAX_PATH];
}FILE_EXE;

class Dir
{
public:
    static void parent();
};

class File
{
public:
    enum { BUFFER_SIZE=10240 };
    enum
    {
#if defined(WINDOWS)
        SEPERATOR = '\\'
#else
        SEPERATOR = '/'
#endif
    };

#if defined(MSC2008)
    typedef struct __stat64 FILE_STATE;
#else
    typedef struct stat     FILE_STATE;
#endif
    static void  split(PATH* rpath, const char* fullpath);
    static void  split_exe(FILE_EXE* file_exe, const char* filename);
    static bool  copy(const char* src, const char* dst, bool mv=false);
    static void  del(const char* dst);
    static void  mkdir(const char* dir);
    static void  deldir(const char* dst);

    static bool  exists(const char* fn);

    static bool  read(u::AutoHeap& content, const TCHAR* fn);
    static bool  write(const TCHAR* fn, const char* buf, DWORD cnt);
    static bool  append(const char* fn, const char* buf, DWORD cnt);
    static bool  last_change_time(U_FILE_TIME* ft, const TCHAR* fn);
#if defined(WINDOWS)
    static DWORD size(const TCHAR* fn, HFILE file=HFILE_ERROR);
#else
    static DWORD size(const TCHAR* fn, FILE* file=NULL);
#endif
    static void list(u::util::Str::Array& files, const char* dir);
    static void cleanup_path(std::string& path, bool uxstyle=false);
    static void cleanup_path(char* path, size_t capacity=MAX_PATH, bool uxstyle=false);
};

U_NAMESPACE_END(io)
U_NAMESPACE_END(u)

#endif
